package com.example.hw_demo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.websocket.MessageHandler;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
//更新创建时间自动
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {//获取当前的时间
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        this.setFieldValByName("createTime", now, metaObject);
        this.setFieldValByName("updateTime", now, metaObject);//因为新增也相当于在更新所以要加上

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        this.setFieldValByName("updateTime", now, metaObject);
    }
}
